package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.SpendPointItem;
import cn.wolfcode.crm.mapper.SpendPointItemMapper;
import cn.wolfcode.crm.mapper.SpendPointMapper;
import cn.wolfcode.crm.query.SpendPointQueryObject;
import cn.wolfcode.crm.service.ISpendPointItemService;
import cn.wolfcode.crm.util.GetMouth;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SpendPointItemServiceImpl implements ISpendPointItemService {
    @Autowired
    private SpendPointItemMapper spendPointItemMapper;
    @Autowired
    private SpendPointMapper spendPointMapper;



    public List<SpendPointItem> selectAll(SpendPointQueryObject qo) {
        if(qo.getSn()!=null){
            Long parentId = spendPointMapper.selectBySn(qo);
            return spendPointItemMapper.selectAll(parentId);
        }
        return null;
    }

    public Boolean selectByName(Long id, String name) {
        SpendPointItem spendPointItem = spendPointItemMapper.selectByName(name);
        return spendPointItem==null||spendPointItem.getId()==id;
    }

    public void saveOrUpdate(SpendPointItem spendPointItem) {
        /*维护关系*/
        if (spendPointItem.getId() == null) {
            spendPointItemMapper.insert(spendPointItem);
        }else{
            spendPointItemMapper.updateByPrimaryKey(spendPointItem);
        }
    }

    public void delete(Long id) {
        if(id!=null){
            spendPointItemMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<SpendPointItem> selectExportData(SpendPointQueryObject qo) {
        return spendPointItemMapper.selectExportData(qo);
    }

    public void importData(MultipartFile file) throws IOException {
        //创建excel文件,将文件导入Excel中
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //获取工作簿
        Sheet sheet = wb.getSheet("spend");
        //获取总行数
        int lastRowNum = sheet.getLastRowNum();
        //迭代每一行
        for (int i = 1; i <= lastRowNum ; i++) {
            //把每行的单元格的数据封装到对象中
            Row row = sheet.getRow(i);
            SpendPointItem  item = new SpendPointItem();
            item.setName(row.getCell(0).getStringCellValue());
            item.setSpendamount(BigDecimal.valueOf(row.getCell(1).getNumericCellValue()));
            item.setSpenddate(row.getCell(2).getDateCellValue());
            item.setSpenduser(row.getCell(3).getStringCellValue());
            item.setInfo(row.getCell(4).getStringCellValue());

            spendPointItemMapper.insert(item);
        }

    }

    @Override
    public BigDecimal selectAmountByDay() throws ParseException {
        //设置时间格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //将当前时间当成参数传递过去
        String dateString = formatter.format(new Date());
        Date date = formatter.parse(dateString);
        return spendPointItemMapper.selectAmountByDay(date);
    }

    @Override
    public BigDecimal selectAmountByMouth() throws ParseException {
        //将月份的第一天和最后一天封装到Map集合中
        Map<String,Date> dates = GetMouth.getMouth();
        return spendPointItemMapper.selectAmountByMouth(dates);
    }

    @Override
    public BigDecimal selectAllAmount() {
        return spendPointItemMapper.selectAllAmount();
    }

    @Override
    public int selectSpendTimes() {
        return spendPointItemMapper.selectSpendTimes();
    }

    @Override
    public List<SpendPointItem> querybyDate(Date beginDate, Date endDate) {
        SpendPointQueryObject qo = new SpendPointQueryObject();
        qo.setBeginDate(beginDate);
        qo.setEndDate(endDate);

        return spendPointItemMapper.querybyDate(qo);
    }

    @Override
    public BigDecimal queryAvgSpend() {
        return spendPointItemMapper.queryAvgSpend();
    }

    @Override
    public BigDecimal queryMaxSpend() {
        return spendPointItemMapper.queryMaxSpend();
    }


    @Override
    public List<Map<String, Object>> items(SpendPointQueryObject qo) {
        return spendPointItemMapper.itmes(qo);
    }

    @Override
    public List<Map<String, Object>> zitems(SpendPointQueryObject qo) {
        return spendPointItemMapper.zitems(qo);
    }


}
